package com.wzt.lianfirecontrol.adapter.xuncha;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.bean.recode.function.xuncha.XunChaQuestionModel;
import com.wzt.lianfirecontrol.utils.StringUtils;
import com.wzt.lianfirecontrol.utils.Utils;

public class XunChaQuestionAdapter extends BaseRecyclerAdapter<XunChaQuestionModel> {

    private Context context;

    private boolean isComplete = false;

    public boolean isComplete() {
        return isComplete;
    }

    public void setComplete(boolean complete) {
        isComplete = complete;
    }

    public XunChaQuestionAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_xuncha_question, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, XunChaQuestionModel data) {
        if (viewHolder instanceof MyViewHolder){
            setItemData((MyViewHolder)viewHolder, data, RealPosition);
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout ll_item_content;
        private TextView tv_title;
        private View v_divdier_line;
        private TextView tv_location;
        private TextView tv_content;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            initItemView(this, itemView);
        }
    }

    private void initItemView(MyViewHolder myViewHolder, View itemView) {
        myViewHolder.ll_item_content = itemView.findViewById(R.id.ll_item_content);
        myViewHolder.tv_title = itemView.findViewById(R.id.tv_title);
        myViewHolder.v_divdier_line = itemView.findViewById(R.id.v_divdier_line);
        myViewHolder.tv_location = itemView.findViewById(R.id.tv_location);
        myViewHolder.tv_content = itemView.findViewById(R.id.tv_content);
    }

    private void setItemData(MyViewHolder viewHolder, XunChaQuestionModel data, int postion) {
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) viewHolder.ll_item_content.getLayoutParams();

        if (isComplete){
            GradientDrawable gradientDrawable = new GradientDrawable();
            gradientDrawable.setColor(context.getResources().getColor(R.color.white));
            viewHolder.v_divdier_line.setVisibility(View.GONE);
            if (postion == getmDatas().size() - 1){
                layoutParams.setMargins(0, 0, 0 ,0);
                gradientDrawable.setCornerRadii(new float[]{0, 0, 0, 0,
                        Utils.dip2px(context, 5), Utils.dip2px(context, 5),
                        Utils.dip2px(context, 5), Utils.dip2px(context, 5)});
            }else {
                gradientDrawable.setCornerRadius(0);
                layoutParams.setMargins(0, 0, 0 , Utils.dip2px(context, 0.5f));
            }
            viewHolder.ll_item_content.setBackground(gradientDrawable);
        }else {
            if (postion == 0){
                layoutParams.setMargins(0, Utils.dip2px(context, 15), 0 ,Utils.dip2px(context, 15));
            }else {
                layoutParams.setMargins(0, 0, 0 ,Utils.dip2px(context, 15));
            }
            viewHolder.v_divdier_line.setVisibility(View.VISIBLE);
            viewHolder.ll_item_content.setBackground(context.getResources().getDrawable(R.drawable.shape_white_bg_frame));
        }
        viewHolder.tv_title.setText(data.getContent());

        StringBuilder stringBuilder = new StringBuilder();
        if (!StringUtils.isEmpty(data.getBuildingName())){
            stringBuilder.append(data.getBuildingName());
        }
        if (!StringUtils.isEmpty(data.getFloorName())){
            stringBuilder.append(data.getFloorName());
        }
        if (!StringUtils.isEmpty(data.getAddress())){
            stringBuilder.append(data.getAddress());
        }
        if (StringUtils.isEmpty(stringBuilder.toString())){
            viewHolder.tv_location.setVisibility(View.GONE);
        }else {
            viewHolder.tv_location.setVisibility(View.VISIBLE);
            viewHolder.tv_location.setText(stringBuilder.toString());
        }
        viewHolder.tv_content.setText(data.getRemark());
    }
}
